<template>
  <div class="container">
    <div class="box kind">
      <div class="header">
        <van-search v-model="value" @click="gosearch" placeholder="请输入搜索关键词" />
      </div>
      <div class="content back">
        <van-sidebar v-model="activeKey" class="typelist">
          <van-sidebar-item v-for="item of kind" :key="item.index" :title="item" @click="onChange" class="typecard"/>
        </van-sidebar>
        <div class="typePro">
          <p class="listtop">商品列表</p>
          <div class="prodesc" v-for="item of promessage" :key="item.index" @click="toDetail(item.proid)">
            <div class="proimg">
              <img :src="item.proimg" alt="">
            </div>
            <div class="promsg">
              <p class="proname">{{ item.name }}</p>
              <p class="prointro">{{ item.desc }}</p>
              <p class="proprice">
                <span class="price">￥{{ item.price }}</span>
                <span class="oldprice">￥{{ item.origin_price }}</span>
                <span class="jia iconfont icon-tianjia" @click.stop="add(item.proid)"></span>
              </p>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import Vue from 'vue'
import { Sidebar, SidebarItem, Search, Toast } from 'vant'
import { getCategory, getCategoryProduct, addCart } from '@/api'
Vue.use(Sidebar)
Vue.use(SidebarItem)
Vue.use(Search)
Vue.use(Toast)
export default {
  data () {
    return {
      activeKey: 0,
      value: '',
      kind: [],
      promessage: [],
      proid: ''
    }
  },
  mounted () {
    getCategory().then(res => {
      this.kind = res.data.data
      this.onChange(0)
    })
  },
  methods: {
    onSearch (val) {
      Toast(val)
    },
    onCancel () {
      Toast('取消')
    },
    gosearch () {
      this.$router.push('/search')
    },
    onChange (index) {
      getCategoryProduct({
        type: this.kind[index]
      }).then(res => {
        this.promessage = res.data.data
      })
    },
    toDetail (proid) {
      console.log(proid)
      // this.$router.push('/detail/' + proid)
      // this.$router.push({ name: 'detail', params: { proid } })
      this.$router.push({ path: '/product/' + proid })
    },
    add (a) {
      // 判断用户是不是登陆状态
      const b = event
      console.log(b)
      if (localStorage.getItem('loginState') === 'ok') {
        // 登陆
        addCart({
          userid: localStorage.getItem('userid'),
          proid: a,
          num: 1
        }).then(res => {
          const { code } = res.data
          if (code === '200') {
            Toast('加入购物车成功')
          } else if (code === '10101') {
            Toast('加入失败')
          }
        })
      } else {
        // 未登录
        this.$router.push('/login')
      }
    }
  }
}
</script>
<style lang="scss">
@import '@/lib/reset.scss';
.kind{
  margin-bottom: 0.66rem;
  .header{
    position: fixed;
    background: white;
  }
}
footer{
  position: fixed;
  bottom: 0;
}
.back{
  background-color: #e5e5e5;
  margin-top: 0.44rem;
}
.typelist{
  float: left;
  // overflow: auto;
  // position: fixed;
  .typecard{
    font-size: 0.13rem;
  }
}
.typePro{
  float: left;
  width: calc(100% - 100px);
  margin: 0.08rem 0 0 0.08rem;
  background-color: white;
  border-radius: 5px;
  box-sizing: border-box;
  // margin-left: 90px;
  .listtop{
    margin: 0.05rem;
    padding: 0.05rem;
    border-bottom: 1px solid #808080;
  }
  .prodesc{
    display: flex;
    padding: 0.18rem 0.1rem 0.18rem 0.08rem;
    .proimg{
      width: 30%;
      img{
        width: 100%;
      }
    }
    .promsg{
      width: 70%;
      padding-left: 0.1rem;
      .prointro{
       overflow: hidden;
       text-overflow:ellipsis;
       white-space: nowrap;
       font-size: 0.12rem;
       color: #acadb0;
       padding: 0.1rem 0 0.14rem 0;
      }
      .proprice{
        font-size: 0.2rem;
        color: #de3e26;
      }
      .oldprice{
        font-size: 0.12rem;
        color: #acadb0;
        text-decoration: line-through;
      }
      .jia{
        font-size: 20px;
        color: #30d2bd;
        border-radius:30%;
        float: right;
      }
    }
  }
}
</style>
